package com.netease.test.controller;

import com.netease.test.bean.Student;
import com.netease.test.utils.ServiceResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/3/8
 */
@Controller
public class ExampleController {

    @RequestMapping(value = "/getAllStudent")
    @ResponseBody
    public ServiceResult getAllStudent() {
        return ServiceResult.success(initData());
    }

    @RequestMapping(value = "/getStudentDetail",method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getStudentDetail(@RequestParam int id) {
        List<Student> studentList = initData();
        for (Student student : studentList) {
            if (student.getId() == id) {
                return ServiceResult.success(student);
            }
        }
        return ServiceResult.successMsg("没有找到对应的用户信息");
    }

    @RequestMapping(value = "/userAuthentication",method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult userAuthentication(@RequestHeader String userName, @RequestParam int id) {

        if (userName.equals("zoudongdong")) {
            List<Student> studentList = initData();
            for (Student student : studentList) {
                if (student.getId() == id) {
                    return ServiceResult.success(student);
                }
            }
            return ServiceResult.successMsg("没有找到对应的用户信息");
        } else {
            return ServiceResult.successMsg("认证失败!!!");
        }
    }

    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult addStudent(@RequestBody Student student) {
        List<Student> studentList = initData();
        studentList.add(student);

        return ServiceResult.successMsg("添加成功");
    }

    public List<Student> initData() {
        List<Student> studentList = new ArrayList<Student>();
        Student student = new Student();
        student.setId(1);
        student.setName("冬冬");
        student.setAge(25);
        student.setGender("女");
        student.setClassName("管理学院");
        studentList.add(student);

        student = new Student();
        student.setId(2);
        student.setName("冬冬2");
        student.setAge(252);
        student.setGender("女");
        student.setClassName("管理学院2");
        studentList.add(student);

        student = new Student();
        student.setId(3);
        student.setName("冬冬3");
        student.setAge(253);
        student.setGender("女");
        student.setClassName("管理学院3");
        studentList.add(student);

        student = new Student();
        student.setId(4);
        student.setName("冬冬4");
        student.setAge(254);
        student.setGender("女");
        student.setClassName("管理学院4");
        studentList.add(student);

        student = new Student();
        student.setId(5);
        student.setName("冬冬5");
        student.setAge(255);
        student.setGender("女");
        student.setClassName("管理学院5");
        studentList.add(student);

        student = new Student();
        student.setId(6);
        student.setName("冬冬6");
        student.setAge(256);
        student.setGender("女");
        student.setClassName("管理学院6");
        studentList.add(student);

        student = new Student();
        student.setId(7);
        student.setName("冬冬7");
        student.setAge(257);
        student.setGender("女");
        student.setClassName("管理学院7");
        studentList.add(student);

        return studentList;
    }
}
